package com.mars.admin.modules.admin;

import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.post.entity.PostComment;
import com.mars.admin.modules.post.service.IPostCommentService;
import com.mars.admin.modules.post.vo.PostConmentVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帖子评论控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/admin/post-comment")
@Tag(name = "后台帖子评论管理", description = "帖子评论相关接口")
public class AdminPostCommentController {

    @Autowired
    private IPostCommentService postCommentService;


    /**
     * 分页查询评论列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询评论列表", description = "分页查询评论列表")
    public Result<Page<PostConmentVO>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "帖子ID") @RequestParam(required = false) Long postId,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "父评论ID") @RequestParam(required = false) Long parentId,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "是否置顶") @RequestParam(required = false) Integer isTop) {
        try {
            Page<PostConmentVO> page = new Page<>(pageNumber, pageSize);
            return Result.success(postCommentService.pagePostComment(page,postId,userId,parentId,status,isTop));
        } catch (Exception e) {
            log.error("分页查询评论列表失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询评论详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询评论详情", description = "根据ID查询评论详情")
    public Result<PostConmentVO> getById(@Parameter(description = "评论ID") @PathVariable Long id) {
        try {
            PostConmentVO comment = postCommentService.getPostCommentWithUserById(id);
            if (comment == null) {
                return Result.error("评论不存在");
            }
            return Result.success(comment);
        } catch (Exception e) {
            log.error("查询评论详情失败，评论ID：{}", id, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 发布评论
     */
    @PostMapping
    @Operation(summary = "发布评论", description = "发布新评论")
    public Result<Boolean> publishComment(@RequestBody PostComment comment) {
        try {
            boolean success = postCommentService.publishComment(comment);
            if (success) {
                return Result.success("发布成功", true);
            } else {
                return Result.error("发布失败");
            }
        } catch (Exception e) {
            log.error("发布评论失败，评论信息：{}", comment, e);
            return Result.error("发布失败：" + e.getMessage());
        }
    }

    /**
     * 回复评论
     */
    @PostMapping("/reply")
    @Operation(summary = "回复评论", description = "回复指定评论")
    public Result<Boolean> replyComment(
            @RequestBody PostComment comment,
            @Parameter(description = "父评论ID") @RequestParam Long parentId,
            @Parameter(description = "被回复用户ID") @RequestParam(required = false) Long replyUserId) {
        try {
            boolean success = postCommentService.replyComment(comment, parentId, replyUserId);
            if (success) {
                return Result.success("回复成功", true);
            } else {
                return Result.error("回复失败");
            }
        } catch (Exception e) {
            log.error("回复评论失败，评论信息：{}，父评论ID：{}，被回复用户ID：{}", comment, parentId, replyUserId, e);
            return Result.error("回复失败：" + e.getMessage());
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论", description = "根据ID删除评论")
    public Result<Boolean> delete(@Parameter(description = "评论ID") @PathVariable Long id) {
        try {
            boolean success = postCommentService.deleteComment(id);
            if (success) {
                return Result.success("删除成功", true);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除评论失败，评论ID：{}", id, e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 查询帖子的热门评论
     */
    @GetMapping("/hot")
    @Operation(summary = "查询热门评论", description = "查询帖子的热门评论")
    public Result<List<PostComment>> getHotComments(
            @Parameter(description = "帖子ID") @RequestParam Long postId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<PostComment> comments = postCommentService.getHotComments(postId, limit);
            return Result.success(comments);
        } catch (Exception e) {
            log.error("查询热门评论失败，帖子ID：{}", postId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询帖子的最新评论
     */
    @GetMapping("/latest")
    @Operation(summary = "查询最新评论", description = "查询帖子的最新评论")
    public Result<List<PostComment>> getLatestComments(
            @Parameter(description = "帖子ID") @RequestParam Long postId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<PostComment> comments = postCommentService.getLatestComments(postId, limit);
            return Result.success(comments);
        } catch (Exception e) {
            log.error("查询最新评论失败，帖子ID：{}", postId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 审核评论
     */
    @PutMapping("/{id}/audit")
    @Operation(summary = "审核评论", description = "审核评论状态")
    public Result<Boolean> auditComment(
            @Parameter(description = "评论ID") @PathVariable Long id,
            @Parameter(description = "审核状态") @RequestParam Integer auditStatus,
            @Parameter(description = "审核原因") @RequestParam(required = false) String auditReason,
            @Parameter(description = "审核人ID") @RequestParam Long auditUserId) {
        try {
            boolean success = postCommentService.auditComment(id, auditStatus, auditReason, auditUserId);
            if (success) {
                return Result.success("审核成功", true);
            } else {
                return Result.error("审核失败");
            }
        } catch (Exception e) {
            log.error("审核评论失败，评论ID：{}，审核状态：{}", id, auditStatus, e);
            return Result.error("审核失败：" + e.getMessage());
        }
    }

    /**
     * 设置评论置顶状态
     */
    @PutMapping("/{id}/top")
    @Operation(summary = "设置评论置顶状态", description = "设置评论是否置顶")
    public Result<Boolean> setTop(
            @Parameter(description = "评论ID") @PathVariable Long id,
            @Parameter(description = "是否置顶：0-否，1-是") @RequestParam Integer isTop) {
        try {
            boolean success = postCommentService.setCommentTop(id, isTop);
            if (success) {
                return Result.success("设置成功", true);
            } else {
                return Result.error("设置失败");
            }
        } catch (Exception e) {
            log.error("设置评论置顶状态失败，评论ID：{}，置顶状态：{}", id, isTop, e);
            return Result.error("设置失败：" + e.getMessage());
        }
    }

    /**
     * 统计帖子的评论数量
     */
    @GetMapping("/count/post")
    @Operation(summary = "统计帖子的评论数量", description = "统计指定帖子的评论数量")
    public Result<Integer> countByPostId(@Parameter(description = "帖子ID") @RequestParam Long postId) {
        try {
            int count = postCommentService.countCommentsByPostId(postId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计帖子的评论数量失败，帖子ID：{}", postId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 统计用户的评论数量
     */
    @GetMapping("/count/user")
    @Operation(summary = "统计用户的评论数量", description = "统计用户的评论数量")
    public Result<Integer> countByUserId(@Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            int count = postCommentService.countCommentsByUserId(userId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计用户的评论数量失败，用户ID：{}", userId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 统计父评论的子评论数量
     */
    @GetMapping("/count/parent")
    @Operation(summary = "统计父评论的子评论数量", description = "统计父评论的子评论数量")
    public Result<Integer> countByParentId(@Parameter(description = "父评论ID") @RequestParam Long parentId) {
        try {
            int count = postCommentService.countCommentsByParentId(parentId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计父评论的子评论数量失败，父评论ID：{}", parentId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 查询待审核的评论
     */
    @GetMapping("/pending-audit")
    @Operation(summary = "查询待审核的评论", description = "分页查询待审核的评论列表")
    public Result<Page<PostComment>> getPendingAuditComments(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<PostComment> page = new Page<>(pageNumber, pageSize);
            Page<PostComment> result = postCommentService.getPendingAuditComments(page);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询待审核的评论失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除评论
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除评论", description = "根据ID列表批量删除评论")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("评论ID列表不能为空");
            }
            boolean success = postCommentService.batchDeleteComments(ids);
            if (success) {
                return Result.success("批量删除成功", true);
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除评论失败，评论ID列表：{}", ids, e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    /**
     * 根据帖子ID删除所有评论
     */
    @DeleteMapping("/post/{postId}")
    @Operation(summary = "删除帖子的所有评论", description = "根据帖子ID删除所有评论")
    public Result<Boolean> deleteByPostId(@Parameter(description = "帖子ID") @PathVariable Long postId) {
        try {
            boolean success = postCommentService.deleteCommentsByPostId(postId);
            if (success) {
                return Result.success("删除成功", true);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("根据帖子ID删除所有评论失败，帖子ID：{}", postId, e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }
} 